package com.hhd.flow.controller;

import com.hhd.flow.vo.Result;
import com.hhd.flow.vo.TranslateVo;
import com.hhd.flow.utils.CommonUtils;
import com.tang.script.ScriptEngine;
import com.tang.script.command.TranslateCommand;
import com.tang.script.result.ScriptResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/flow/formula")
public class FormulaController {

    @Autowired
    ScriptEngine scriptEngine;

    @PostMapping("/translate")
    public Result translate(@RequestBody TranslateVo translateVo) {
        if (CommonUtils.isEmpty(translateVo.getFormula())) {
            return Result.ok(translateVo);
        }
        TranslateCommand command = scriptEngine.getScriptCommandBuilder().newTranslateCommand()
                .scriptText(translateVo.getFormula())
                .build();
        ScriptResult<String> stringScriptResult = scriptEngine.executeScript(command);
        translateVo.setFormulaText(stringScriptResult.getResult());
        return Result.ok(translateVo);
    }
}
